// Teacher页面获取数据
import Mock from "mockjs"
import { param2Obj } from '../utils/paramsUrl.js'
var List = []  //保存所有数据
for (var i = 0; i < 100; i++) {
    List.push({
        id: Mock.mock('@guid'),
        name: Mock.mock('@cname'),
        course: '幼儿英语入门教程',
        price: Mock.mock('@integer(60, 500)'), //价格
        num: Mock.mock('@integer(1, 500)'), //销量
        person: Mock.mock('@first()'),//上传人
        uptime: Mock.mock('@datetime'), //上传时间
    })
}

export default {
    // 对应属性 获取数据
    getData: config => {
        const { page, pageSize, searchVal } = param2Obj(config.url)
        console.log(page, pageSize);
        // 搜索
        var newListarr = List.filter(item => {
            return item.name.indexOf(searchVal) != -1
        })
        // 分页
        var newList = newListarr.filter((item, index) => {
            return (page - 1) * pageSize <= index && index < page * pageSize
        })
        // List = newList
        return {
            code: 200,
            msg: '查询成功',
            data: newList,
            total: newListarr.length
        }
    },
    deleteData: config => {
        console.log('config', config);
        const { id } = JSON.parse(config.body)
        for (var i = 0; i < List.length; i++) {
            if (List[i].id == id) {
                List.splice(i, 1)
            }
        }
        return {
            code: 200,
            msg: '删除成功',
            data: null
        }
    },
    batchData: config => {
        const { ids } = JSON.parse(config.body)
        var newList = List.filter(item => {
            return ids.indexOf(item.id) == -1
        })
        List = newList
        return {
            code: 200,
            msg: '批量删除成功',
            data: null
        }
    },
    addData: config => {
        const { name, course, price, num, person } = JSON.parse(config.body)
        List.unshift({
            id: Mock.mock('@guid'),
            name: name,
            course: course,
            price: price, //价格
            num: num, //销量
            person: person,//上传人
            uptime: Mock.mock('@datetime'), //上传时间
        })
        return {
            code: 200,
            msg: '添加成功',
            data: null
        }
    },
    updateData: config => {
        const { id, name, course, price, num, person } = JSON.parse(config.body)
        for (var i = 0; i < List.length; i++) {
            if (List[i].id === id) {
                List[i].name = name
                List[i].course = course
                List[i].price = price
                List[i].num = num
                List[i].person = person
                List[i].uptime = Mock.mock('@datetime')

            }
        }
        return {
            code: 200,
            msg: '修改成功',
            data: null

        }
    }
}
